package com.cargroup.controller;

import com.cargroup.bean.Car;
import com.cargroup.service.CarService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@RequestMapping("/car")
@Controller
public class CarController {
    @Autowired
    private CarService carService;

    @RequestMapping("/insertCar")
//    @ResponseBody
    public String insertCar(Car car) {
        carService.insertCar(car);
        return "redirect:/car/manager-cargetall";
    }

    @RequestMapping("/deleteCar")
    public String deleteCar(Integer id) {
        System.out.println(id);
        carService.deleteCar(id);
        return "redirect:/car/manager-cargetall";
    }

    @RequestMapping("/editCar")
//    @ResponseBody
    public String editCar(Car car) {
        System.out.println(car);
        carService.editCar(car);
        return "redirect:/car/manager-cargetall";
    }

    @RequestMapping("/getall")
    public String getTeachers(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        // 紧跟着的查询就是分页查询
        PageHelper.startPage(pn, 5);
        List<Car> list = carService.getallcar();
        // 将查询的结果放在PageInfo中，这个PageInfo就用很多能用的
        //第二份传入连需要显示的页码
        PageInfo<Car> info = new PageInfo(list, 5);
//        System.out.println("当前页码：" + info.getPageNum());
//        System.out.println("总页码：" + info.getPages());
//        System.out.println("总记录数：" + info.getTotal());
//        System.out.println("当前页有几条记录：" + info.getSize());
//        System.out.println("当前页的pageSize：" + info.getPageSize());
//        System.out.println("当前页的前一页：" + info.getPrePage());
//        System.out.println("结果：" + info.getList());// 查询结果
        int[] nums = info.getNavigatepageNums();

        model.addAttribute("info", info);
        return "user-car";
    }

    @RequestMapping("/manager-cargetall")
    public String managercargetall(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        // 紧跟着的查询就是分页查询
        PageHelper.startPage(pn, 5);
        List<Car> list = carService.getallcar();
        // 将查询的结果放在PageInfo中，这个PageInfo就用很多能用的
        //第二份传入连需要显示的页码
        PageInfo<Car> info = new PageInfo(list, 5);
//        System.out.println("当前页码：" + info.getPageNum());
//        System.out.println("总页码：" + info.getPages());
//        System.out.println("总记录数：" + info.getTotal());
//        System.out.println("当前页有几条记录：" + info.getSize());
//        System.out.println("当前页的pageSize：" + info.getPageSize());
//        System.out.println("当前页的前一页：" + info.getPrePage());
//        System.out.println("结果：" + info.getList());// 查询结果
        int[] nums = info.getNavigatepageNums();

        model.addAttribute("info", info);
        return "manager-car";
    }


}
